package com.csl.hotel.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csl.hotel.entity.RyJwryzsdj;
import com.csl.hotel.entity.RyTpxx;
import com.csl.hotel.entity.RyZsdjhzxx;
/**
 * json 工具类
 * @author liucs
 * 
 */
public class JsonUtil {

	public JsonUtil() {
	}
	
	public static String convertObj2json(Object obj){
		String jsonstr=JSONObject.toJSONString(obj,true);
		
		return jsonstr;
	}
	
	public static void main(String[] args) {
		String djxxJson = genJwryJson();
		System.out.println(djxxJson);
	}
	/**
	 * 生成接口所需json 参数
	 * @param djxx
	 * @return
	 */
	public static String convertJwryDjxxJson(RyJwryzsdj djxx){
		StringBuffer sb=new StringBuffer();
		sb.append("{").append("\"djxx\":");
		String djxxjson=convertObj2json(djxx);
		sb.append(djxxjson);
		
		//图片信息
		RyTpxx tpxx=new RyTpxx("", "100000", "1");
		RyTpxx tpxx2=new RyTpxx("", "100001", "1");
		List<RyTpxx> tpxxs=new ArrayList<RyTpxx>();
		tpxxs.add(tpxx);
		tpxxs.add(tpxx2);
		String tpxxjson=JSONArray.toJSONString(tpxxs, true);
		sb.append(",").append("\"tpxx\":").append(tpxxjson);
		
		//户主信息
		RyZsdjhzxx hzxx=new RyZsdjhzxx(djxx.getLsdwhz(),djxx.getLsdwhzdh());
		String hzxxjson=convertObj2json(hzxx);
		sb.append(",").append("\"fzxx\":");
		sb.append(hzxxjson);
		
		sb.append("}");
		return sb.toString();
	}
	/**
	 * 转换为接口方法需要的 json 格式数据
	 * @return
	 */
	public static String genJwryJson() {
		StringBuffer sb=new StringBuffer();
		sb.append("{").append("\"djxx\":");
		RyJwryzsdj djxx=new RyJwryzsdj();
		djxx.setYwxm("jack Ma");
		djxx.setZwxm("刘佳");
		djxx.setXb("1");
		djxx.setCsrq("19750814");
		djxx.setZjzl("3");
		djxx.setZjhm("1234567");
		djxx.setCssj("201710181945");
		
		String djxxjson=convertObj2json(djxx);
		sb.append(djxxjson);
		
		//图片信息
		RyTpxx tpxx=new RyTpxx("Brett", "rbp4", "1");
		RyTpxx tpxx2=new RyTpxx("Brett", "rbp5", "2");
		List<RyTpxx> tpxxs=new ArrayList<RyTpxx>();
		tpxxs.add(tpxx);
		tpxxs.add(tpxx2);
		
		String tpxxjson=JSONArray.toJSONString(tpxxs, true);
	//	System.out.println("tpxx:"+tpxxjson);
		sb.append(",").append("\"tpxx\":").append(tpxxjson);
		
		
		//户主信息
		RyZsdjhzxx hzxx=new RyZsdjhzxx("tony","13568956874");
		String hzxxjson=convertObj2json(hzxx);
	//	System.out.println("hzxx:"+hzxxjson);
		sb.append(",").append("\"fzxx\":");
		sb.append(hzxxjson);
		
		sb.append("}");
		return sb.toString();
	}
	
	/**
	 * 实现字符串转换为json数据格式
	 * @param str 格式为："id=0dc78df03b204296800bb42c0b28afc3&sn=822491080210041&name=荥阳京城王宁牙科诊所"
	 * @return json 字符串格
	 * @author liucs
	 */
	public static String parseStrToJson(String str){
		String[] strArr=str.split("&");
		StringBuffer sb=new StringBuffer("{");
		for(String str1:strArr){
			String jsonstr="\""+str1.split("=")[0]+"\":\""+str1.split("=")[1]+"\"";
			sb.append(jsonstr).append(",");
		}
		String retstr=sb.toString();
		String newstr=retstr.substring(0,retstr.length()-1);
		String retJson=newstr+"}";
		try {
			retJson=URLDecoder.decode(retJson, "UTF-8");
			System.out.println(retJson);
			//String newjson=retJson.replace("%5B%5D", "");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			System.err.println("解析字符串出错！");
		}
		return retJson;
	}

}
